package com.zmn.plat.admin.controller.part.category.second;

import java.util.Objects;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.plat.admin.controller.part.PartBaseController;
import com.zmn.plat.model.entity.part.PartCategory;

/**
 * @author : admin
 * @since : 2018/10/16 11:23
 */

@Controller
@RequestMapping("/part/category/second")
public class PartCategorySecondListController extends PartBaseController {

    private Logger logger = LoggerFactory.getLogger(PartCategorySecondListController.class);

    @RequestMapping("/list/{parentId}")
    public String partCategoryList(Model model, @PathVariable("parentId") Integer parentId) {

        model.addAttribute("parentPartCategory", partCategoryService.findByKey(parentId));
        model.addAttribute("levelOneCategoryList", partCategoryService.getPartCategorySimpleList(GlobalConsts.TOP_ID));
        model.addAttribute("statuses", GlobalDict.getStatusList(false));

        return "part/category/second/list";
    }

    @RequestMapping(value = "/list/addEdit")
    @RequiresPermissions("/part/category/second/list/addEdit.action")
    public String addedit(@RequestParam(value = "parentId") Integer parentId,
        @RequestParam(value = "categId", required = false) Integer categId, Model model) {

        PartCategory parentPartCategory = partCategoryService.findByKey(parentId);

        PartCategory newCategory = new PartCategory();
        newCategory.setParentId(parentId);
        newCategory.setStatus((short)GlobalDict.STATUS_ENABLE);
        PartCategory partCategory = Objects.isNull(categId) ? newCategory : partCategoryService.findByKey(categId);

        model.addAttribute("partCategory", partCategory);
        model.addAttribute("parentPartCategory", parentPartCategory);
        model.addAttribute("statuses", GlobalDict.getStatusList(false));

        return "part/category/second/addedit";
    }

}
